package com.iotek.bookstore.modle.dao.impl;

import com.iotek.bookstore.modle.entity.TitleTwo;
import com.iotek.bookstore.util.PreparedStatementSetter;
import com.iotek.bookstore.util.RsCallBackHandler;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

public class TitleTwoDaoImpl extends MyBookstoreDaoImpl<TitleTwo>{

    /*
    * 添加二级分类标题
    * */
    @Override
    public int insert(final TitleTwo titleTwo) {
        String sql="insert into titletwo (id,titleOneId,title) values(?,?,?)";
        return this.update(sql, new PreparedStatementSetter() {
            public void setValues(PreparedStatement pstmt) throws SQLException {
                pstmt.setInt(1,titleTwo.getId());
                pstmt.setInt(2,titleTwo.getTitleOneId());
                pstmt.setString(3,titleTwo.getTitle());
            }
        });
    }

    /*
    * 查询所有的二级分类标题
    * */
    @Override
    public List<TitleTwo> queryAll() {
        String sql="select * from titletwo";
        this.query(sql, null, new RsCallBackHandler() {
            public void processRs(ResultSet rs) throws SQLException {
               createTitleTwo(rs);
            }
        });
        return list;
    }


    /*
    * 根据一级分类标题ID查询相应的二级分类标题
    * */
    @Override
    public List<TitleTwo> query(final int id) {
        String sql="select id,titleOneId,title from titletwo where titleOneId=?";
        this.query(sql, new PreparedStatementSetter() {
            public void setValues(PreparedStatement pstmt) throws SQLException {
                pstmt.setInt(1, id);
            }
        }, new RsCallBackHandler() {
            public void processRs(ResultSet rs) throws SQLException {
                createTitleTwo(rs);
            }
        });
        return list;
    }


    /*
    * 根据二级分类标题ID删除对应的二级分类标题
    * */
    @Override
    public int delete(final int id) {
        String sql="delete from titletwo where id=?";
        return this.update(sql, new PreparedStatementSetter() {
            public void setValues(PreparedStatement pstmt) throws SQLException {
                pstmt.setInt(1,id);
            }
        });
    }

    /*
    * 修改二级分类标题
    * */
    @Override
    public int update(final TitleTwo titleTwo) {
        String sql="update titletwo set title=? where id=?";
        return this.update(sql, new PreparedStatementSetter() {
            public void setValues(PreparedStatement pstmt) throws SQLException {
                pstmt.setString(1,titleTwo.getTitle());
                pstmt.setInt(2,titleTwo.getId());
            }
        });
    }

    /*
    * 生成查询二级分类标题通用TitleTwo的实例
    * */
    private void createTitleTwo(ResultSet rs)throws SQLException{
        while (rs.next()){
            TitleTwo titleTwo=new TitleTwo();
            titleTwo.setId(rs.getInt(1));
            titleTwo.setTitleOneId(rs.getInt(2));
            titleTwo.setTitle(rs.getString(3));
            list.add(titleTwo);
        }
    }
}
